package cn.nfwx.tv.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import cn.nfwx.tv.R;
import cn.nfwx.tv.db.AppDatabase;
import cn.nfwx.tv.db.DownProgramDao;
import cn.nfwx.tv.db.DownProgramModel;
import cn.nfwx.tv.db.TvAppointment;
import cn.nfwx.tv.util.TimeUtil;
import cn.nfwx.tv.vo.AppiontmentSection;


/**
 * Created by zb.liu on 2017/11/1.
 */

public class AppiontmentAdapter extends BaseSectionQuickAdapter<AppiontmentSection, BaseViewHolder> {

    private SimpleDateFormat dateFormat;
    private boolean isDelete;
    private boolean isSelectAll = false;
    private OnCheckedChangeDelListener onCheckedChangeDelListener;

    public AppiontmentAdapter() {
        super(R.layout.fragment_appiontment_item, R.layout.fragment_appiontment_date_item, null);
        dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    }

    @Override
    protected void convertHead(BaseViewHolder helper, AppiontmentSection item) {
        helper.setText(R.id.appiontment_item_date, item.header);

    }

    @Override
    protected void convert(final BaseViewHolder helper, AppiontmentSection item) {
        if (item.t instanceof TvAppointment) {
            final TvAppointment tvAppointment = (TvAppointment) item.t;
            tvAppointment.setLive(false);
            helper.setText(R.id.appiontment_item_channel, tvAppointment.getChannel_name());
            helper.setText(R.id.appiontment_item_program, tvAppointment.getProgram_name());
            try {
                Date dateStart = dateFormat.parse(tvAppointment.getProgram_time());
                Date dateEnd = null;
                if (!TextUtils.isEmpty(tvAppointment.getProgram_end_time())) {
                    dateEnd = dateFormat.parse(tvAppointment.getProgram_end_time());
                }
                helper.setTextColor(R.id.appiontment_item_time, helper.itemView.getContext().getResources().getColor(R.color.setting_content_text));
                helper.setTextColor(R.id.appiontment_item_program, helper.itemView.getContext().getResources().getColor(R.color.setting_content_text));
                helper.setTextColor(R.id.appiontment_item_channel, helper.itemView.getContext().getResources().getColor(R.color.setting_content_text));
                helper.setTextColor(R.id.appoint_statue, helper.itemView.getContext().getResources().getColor(R.color.setting_content_text));
                helper.setText(R.id.appiontment_item_time, TimeUtil.getFormatHHmmDate(dateStart));
                if (System.currentTimeMillis() < dateStart.getTime()) {
                    helper.setText(R.id.appoint_statue, "");
                    helper.setVisible(R.id.btn_appiontment_downlaod, false);
                    helper.setVisible(R.id.appoint_statue, false);
                } else if (dateEnd != null && System.currentTimeMillis() <= dateEnd.getTime() && System.currentTimeMillis() >= dateStart.getTime()) {
                    helper.setVisible(R.id.btn_appiontment_downlaod, false);
                    helper.setVisible(R.id.appoint_statue, true);
                    helper.setText(R.id.appoint_statue, "直播中");
                    helper.setTextColor(R.id.appiontment_item_time, helper.itemView.getContext().getResources().getColor(R.color.down_btn_bg));
                    helper.setTextColor(R.id.appiontment_item_program, helper.itemView.getContext().getResources().getColor(R.color.down_btn_bg));
                    helper.setTextColor(R.id.appiontment_item_channel, helper.itemView.getContext().getResources().getColor(R.color.down_btn_bg));
                    helper.setTextColor(R.id.appoint_statue, helper.itemView.getContext().getResources().getColor(R.color.down_btn_bg));
                    tvAppointment.setLive(true);
                } else if (dateEnd != null && System.currentTimeMillis() > dateEnd.getTime()) {
                    if (TextUtils.isEmpty(tvAppointment.getUrl())) {
                        Button btn_appiontment_downlaod = helper.getView(R.id.btn_appiontment_downlaod);
                        btn_appiontment_downlaod.setClickable(false);
                        btn_appiontment_downlaod.setBackgroundDrawable(helper.itemView.getContext().getResources().getDrawable(R.drawable.download_btn_false));
                        btn_appiontment_downlaod.setVisibility(View.VISIBLE);
                    } else {
                        if (AppDatabase.getInstance(mContext).isOpen()) {
                            DownProgramDao downProgramDao = AppDatabase.getInstance(mContext).programDao();
                            List<DownProgramModel> downProgramModels = downProgramDao.queryAllByUrl(tvAppointment.getUrl());
                            if (downProgramModels.isEmpty()) {
                                helper.setVisible(R.id.btn_appiontment_downlaod, true);
                                helper.setVisible(R.id.appoint_statue, false);
                                Button btn_appiontment_downlaod = helper.getView(R.id.btn_appiontment_downlaod);
                                btn_appiontment_downlaod.setClickable(true);
                                btn_appiontment_downlaod.setBackgroundDrawable(helper.itemView.getContext().getResources().getDrawable(R.drawable.download_btn));
                            } else if (downProgramModels.get(0).getStats() == DownProgramModel.STATS_SUCCESS) {
                                helper.setVisible(R.id.btn_appiontment_downlaod, false);
                                helper.setVisible(R.id.appoint_statue, true);
                                helper.setText(R.id.appoint_statue, "已下载");
                                helper.setTextColor(R.id.appoint_statue, helper.itemView.getContext().getResources().getColor(R.color.setting_content_text));
                            } else {
                                helper.setVisible(R.id.btn_appiontment_downlaod, false);
                                helper.setVisible(R.id.appoint_statue, true);
                                helper.setText(R.id.appoint_statue, "下载中");
                                helper.setTextColor(R.id.appoint_statue, helper.itemView.getContext().getResources().getColor(R.color.setting_content_text));
                            }
                        }
                        helper.setTextColor(R.id.appiontment_item_time, helper.itemView.getContext().getResources().getColor(R.color.setting_content_text));
                        helper.setTextColor(R.id.appiontment_item_program, helper.itemView.getContext().getResources().getColor(R.color.setting_content_text));
                        helper.setTextColor(R.id.appiontment_item_channel, helper.itemView.getContext().getResources().getColor(R.color.setting_lab_text));
                    }

                    helper.addOnClickListener(R.id.btn_appiontment_downlaod);
                } else if (dateEnd == null && System.currentTimeMillis() >= dateStart.getTime()) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(dateStart);
                    int startYear = calendar.get(Calendar.YEAR);
                    int startDay = calendar.get(Calendar.DAY_OF_YEAR);

                    calendar.setTimeInMillis(System.currentTimeMillis());
                    int currentYear = calendar.get(Calendar.YEAR);
                    int currentDay = calendar.get(Calendar.DAY_OF_YEAR);

                    if (startYear == currentYear && currentDay == startDay) {
                        helper.setVisible(R.id.btn_appiontment_downlaod, false);
                        helper.setVisible(R.id.appoint_statue, true);
                        helper.setText(R.id.appoint_statue, "直播中");
                        helper.setTextColor(R.id.appiontment_item_time, helper.itemView.getContext().getResources().getColor(R.color.down_btn_bg));
                        helper.setTextColor(R.id.appiontment_item_program, helper.itemView.getContext().getResources().getColor(R.color.down_btn_bg));
                        helper.setTextColor(R.id.appiontment_item_channel, helper.itemView.getContext().getResources().getColor(R.color.down_btn_bg));
                        helper.setTextColor(R.id.appoint_statue, helper.itemView.getContext().getResources().getColor(R.color.down_btn_bg));
                        tvAppointment.setLive(true);
                    } else if (startYear < currentDay || startDay < currentDay) {
                        if (TextUtils.isEmpty(tvAppointment.getUrl())) {
                            Button btn_appiontment_downlaod = helper.getView(R.id.btn_appiontment_downlaod);
                            btn_appiontment_downlaod.setClickable(false);
                            btn_appiontment_downlaod.setBackgroundDrawable(helper.itemView.getContext().getResources().getDrawable(R.drawable.download_btn_false));
                            btn_appiontment_downlaod.setVisibility(View.VISIBLE);
                        } else {
                            if (AppDatabase.getInstance(mContext).isOpen()) {
                                DownProgramDao downProgramDao = AppDatabase.getInstance(mContext).programDao();
                                List<DownProgramModel> downProgramModels = downProgramDao.queryAllByUrl(tvAppointment.getUrl());
                                if (downProgramModels.isEmpty()) {
                                    helper.setVisible(R.id.btn_appiontment_downlaod, true);
                                    helper.setVisible(R.id.appoint_statue, false);
                                    Button btn_appiontment_downlaod = helper.getView(R.id.btn_appiontment_downlaod);
                                    btn_appiontment_downlaod.setClickable(true);
                                    btn_appiontment_downlaod.setBackgroundDrawable(helper.itemView.getContext().getResources().getDrawable(R.drawable.download_btn));
                                } else if (downProgramModels.get(0).getStats() == DownProgramModel.STATS_SUCCESS) {
                                    helper.setVisible(R.id.btn_appiontment_downlaod, false);
                                    helper.setVisible(R.id.appoint_statue, true);
                                    helper.setText(R.id.appoint_statue, "已下载");
                                    helper.setTextColor(R.id.appoint_statue, helper.itemView.getContext().getResources().getColor(R.color.setting_content_text));
                                } else {
                                    helper.setVisible(R.id.btn_appiontment_downlaod, false);
                                    helper.setVisible(R.id.appoint_statue, true);
                                    helper.setText(R.id.appoint_statue, "下载中");
                                    helper.setTextColor(R.id.appoint_statue, helper.itemView.getContext().getResources().getColor(R.color.setting_content_text));
                                }
                            }

                            helper.setTextColor(R.id.appiontment_item_time, helper.itemView.getContext().getResources().getColor(R.color.setting_content_text));
                            helper.setTextColor(R.id.appiontment_item_program, helper.itemView.getContext().getResources().getColor(R.color.setting_content_text));
                            helper.setTextColor(R.id.appiontment_item_channel, helper.itemView.getContext().getResources().getColor(R.color.setting_lab_text));
                        }

                        helper.addOnClickListener(R.id.btn_appiontment_downlaod);
                    }
                }
                if (isDelete) {
                    helper.setVisible(R.id.cb_appiont, true);
                } else {
                    helper.setVisible(R.id.cb_appiont, false);
                }
                CheckBox checkBox = helper.getView(R.id.cb_appiont);
                checkBox.setChecked(tvAppointment.isCheck());
                checkBox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tvAppointment.setCheck(!tvAppointment.isCheck());
                        if (onCheckedChangeDelListener != null) {
                            onCheckedChangeDelListener.onCheckedAllChanged(isDeleteAllDatas());
                        }
                    }
                });
                if (onCheckedChangeDelListener != null) {
                    onCheckedChangeDelListener.onCheckedAllChanged(isDeleteAllDatas());
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    public void setDelete(boolean delete) {
        this.isDelete = delete;
        notifyDataSetChanged();
    }

    public boolean isDelete() {
        return isDelete;
    }

    public boolean isSelectAll() {
        return isSelectAll;
    }

    public void setSelectAll(boolean selectAll) {
        isSelectAll = selectAll;
        for (AppiontmentSection appiontmentSection : getData()) {
            if (appiontmentSection.t instanceof TvAppointment) {
                TvAppointment tvAppointment = (TvAppointment) appiontmentSection.t;
                tvAppointment.setCheck(selectAll);
            }
        }
        notifyDataSetChanged();
    }

    public void setOnCheckedChangeDelListener(OnCheckedChangeDelListener onCheckedChangeDelListener) {
        this.onCheckedChangeDelListener = onCheckedChangeDelListener;
    }

    public interface OnCheckedChangeDelListener {
        // 是否全选中
        void onCheckedAllChanged(boolean stats);
    }


    /**
     * 获取删除列表
     *
     * @return
     */
    public List<TvAppointment> getDeleteDatas() {
        List<TvAppointment> tvAppointments = new ArrayList<>();
        for (AppiontmentSection appiontmentSection : getData()) {
            if (appiontmentSection.t instanceof TvAppointment) {
                TvAppointment tvAppointment = (TvAppointment) appiontmentSection.t;
                if (tvAppointment.isCheck()) {
                    tvAppointments.add(tvAppointment);
                }
            }
        }
        return tvAppointments;
    }

    /**
     * 获取删除列表
     *
     * @return
     */
    public boolean isDeleteAllDatas() {
        boolean stats = true;
        for (AppiontmentSection appiontmentSection : getData()) {
            if (appiontmentSection.t instanceof TvAppointment) {
                TvAppointment tvAppointment = (TvAppointment) appiontmentSection.t;
                if (!tvAppointment.isCheck()) {
                    stats = false;
                    break;
                }
            }
        }
        isSelectAll = stats;
        return stats;
    }
}
